package cn.sptpc.dao.impl;

import cn.sptpc.dao.DepartmentDao;
import cn.sptpc.entity.java.Department;
import cn.sptpc.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDaoI implements DepartmentDao {

    private DBUtil dbUtil = DBUtil.getDbUtil();
    
    /**
     * 插入一条记录
     *
     * @param department
     * @return
     */
    @Override
    public Integer insertDepartment(Department department) {
        Integer i = null;
        Connection con = dbUtil.getConnection();
        String sql = "insert into department (department_name, department_introduction) values (?,?)";
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setString(1,department.getDepartmentName());
            pstm.setString(2,department.getDepartmentIntroduction());
            i = pstm.executeUpdate();
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过id更新一条记录
     *
     * @param department
     * @return
     */
    @Override
    public Integer updataDepartment(Department department) {
        return null;
    }

    /**
     * 获取行总数
     *
     * @return
     */
    @Override
    public Integer countAllDepartment() {
        Integer i = null;
        Connection con = dbUtil.getConnection();
        String sql = "select count(id) from department";
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();
            while(rs.next()){
                i = rs.getInt(1);
            }
            return i;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 通过id 查询一条记录
     *
     * @param id
     * @return
     */
    @Override
    public Department queryDepartment(int id) {
        Connection con = dbUtil.getConnection();
        String sql = "select * from department where id = ? ";
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setInt(1,id);
            ResultSet rs = pstm.executeQuery();
            Department dpart = new Department();
            while (rs.next()){
                dpart.setDepartmentName(rs.getString("department_name"));
                dpart.setId(rs.getLong("id"));
                dpart.setDepartmentIntroduction(rs.getString("department_introduction"));
            }
            return dpart;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询所有部门
     *
     * @return
     */
    @Override
    public List<Department> getAllDepartment() {
        Connection con = dbUtil.getConnection();
        String sql = "select * from department";
        PreparedStatement pstm = null;
        try {
            pstm = con.prepareStatement(sql);
            ResultSet rs = pstm.executeQuery();
            List<Department> departments = new ArrayList<>();
            while (rs.next()){
                Department dpart = new Department();
                dpart.setDepartmentName(rs.getString("department_name"));
                dpart.setId(rs.getLong("id"));
                dpart.setDepartmentIntroduction(rs.getString("department_introduction"));
                departments.add(dpart);
            }
            return departments;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Department queryDepartment(String name) {
        Connection con = dbUtil.getConnection();
        String sql = "select * from department where department_name = ? ";
        try {
            PreparedStatement pstm = con.prepareStatement(sql);
            pstm.setString(1,name);
            ResultSet rs = pstm.executeQuery();
            Department dpart = new Department();
            while (rs.next()){
                dpart.setDepartmentName(rs.getString("department_name"));
                dpart.setId(rs.getLong("id"));
                dpart.setDepartmentIntroduction(rs.getString("department_introduction"));
            }
            return dpart;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
